﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infra.Entity;

namespace Infra.Common.DAO
{
    public class GrupoDAL
    {
        public static void Salvar(SEGGROUP grupo)
        {
            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                if (grupo.CODGROUP != 0)
                {
                    //Alterar o utilizador
                    var g = dc.SEGGROUPs.Single(gp => gp.CODGROUP == grupo.CODGROUP);
                    g.STRGROUP = grupo.STRGROUP;
                    dc.SubmitChanges();
                }
                else
                {
                    //Gravar utilizador
                    dc.SEGGROUPs.InsertOnSubmit(grupo);
                    dc.SubmitChanges();
                }
            }
        }



        public static void Excluir(int idGrupo)
        {
            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                var u = dc.SEGGROUPs.Single(us => us.CODGROUP == idGrupo);
                dc.SEGGROUPs.DeleteOnSubmit(u);
                dc.SubmitChanges();
            }
        }




        public static List<SEGGROUP> Pesquisar(int idGrupo, string grupo)
        {
            List<SEGGROUP> ListGrupo = null;

            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                var g = from gp in dc.SEGGROUPs
                        where ((idGrupo.Equals(0) || idGrupo.Equals(gp.CODGROUP)) &&
                        (grupo.Equals(string.Empty) || grupo.Equals(gp.STRGROUP.Contains(grupo))))
                        select gp;
                ListGrupo = g.ToList();
            }

            return ListGrupo;
        }


        public static List<SEGGROUP> Pesquisar()
        {
            List<SEGGROUP> ListGrupo = null;

            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                var g = from gp in dc.SEGGROUPs
                        select gp;
                ListGrupo = g.ToList();
            }

            return ListGrupo;
        }


    }
}
